const app = getApp()
const util = require('../../../utils/util')
Page({
  data: {
    spinShow: false,
    account: '',
    password: '',
    choose: 's',
    disabled: false,
    btnText: "获取验证码",
    mobile: "",
    type: "primary",
    code: "",
    isAgree: false,
  },
  // 处理输入事件
  input(event) {
    const { name, value } = event.detail; // 获取输入框的 name 和 value
    if (name === 'name') {
      this.setData({
        mobile: value // 更新 mobile
      });
    } else if (name === 'pwd') {
      this.setData({
        password: value // 更新 password
      });
    }
  },
  formSubmit: function (e) {
    let _this = this
    let form = e.detail.value
    _this.setData({
      spinShow: true,
      account: form['name'],
      password: form['pwd']
    });
    wx.setStorageSync('account', form['name'])
    if (_this.data.isAgree == true) {
      wx.login({
        success(wxres) {
          if (wxres.code) {
            e.detail.value.code = wxres.code
            let login_url = ''
            let gerInfo_url = ''
            if (_this.data.choose == 't') {
              login_url = '/login_teacher?account=' + _this.data.account + '&password=' + _this.data.password
              gerInfo_url = '/get_teacher_detail?account='
              wx.setStorageSync('people', 'teacher')
            } else if (_this.data.choose == 's') {
              login_url = '/login_student?account=' + _this.data.account + '&password=' + _this.data.password
              gerInfo_url = '/get_student_detail?account='
              wx.setStorageSync('people', 'student')
            }
            app.formGet(login_url)
              .then(res => {
                _this.setData({
                  spinShow: false
                });
                console.log('formGet 正在登录', res); // myCode
                if (res.code == 1) {
                  wx.setStorageSync('myCode', res['message'])
                  app.formGet(gerInfo_url + _this.data.account + '&code=' + res['message']).then(res => {
                    console.log('formGet 登录成功 设置信息', res); // myCode
                    let data = res
                    app.setInfo(data)
                    app.globalData.hadInfo = true
                  }).then(function () {
                    app.message('登录成功！', 'success')
                    setTimeout(() => {
                      wx.reLaunch({
                        url: '/pages/index/index',
                      })
                    }, 1000)

                  });
                } else {
                  app.message(res.message, 'error')
                }
              }).catch(e => {
                _this.setData({
                  spinShow: false
                });
                app.message(e, 'error')
              })
          } else {
            app.message(res.errMsg, 'error')
          }
        }
      })
    } else {
      wx.showModal({
        title: '请先阅读用户协议',
        content: '您需要先阅读并同意《用户协议》，才能登录',
        complete: (res) => {
          if (res.cancel) {

          }

          if (res.confirm) {

          }
        }
      })
    }

  },

  register: function (e) {
    wx.navigateTo({
      url: "../register/index"
    })
  },

  choose_t: function () {
    this.setData({
      choose: 't'
    })
  },

  choose_s: function () {
    this.setData({
      choose: 's'
    })
  },

  protocol: function () {
    wx.showToast({
      title: 'No user agreement!',
      icon: 'error',
    })
  },

  clickUserAgreement: function () {
    this.setData({
      isAgree: !this.data.isAgree,
    })
  },

  protocol: function () {
    wx.navigateTo({
      url: '../userknow/userknow',
    })
  },
})